﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;

using System.Query;
using System.Data.DLinq;
//using System.Expressions;
using DataAccesLayer;
namespace WpfApplicationExperimental
{
    public class DocumentViewController
    {
        public IListView listView;

        DataAccesLayer.DocumentDataAccesLayer accesLayer;

        public DocumentViewController()
        {
            accesLayer = new DataAccesLayer.EQualDataLayerFactory().GetDocumentAccesLayer();
        }

        public void SubmitChanges()
        {
           accesLayer.SubmitChanges();
        }

        public void RequestNewDocumentCreation(DataAccesLayer.Document document)
        {
            try
            {
                if (accesLayer.DoesDocumentExist(document))
                {
                   accesLayer.UpdateDocument(document);
                }
                else
                {
                   accesLayer.AddNewDocument(document);
                }
            }
            catch (Exception e)
            {

            }

            listView.UpdateList();
        }

        public void RequestDocumentDelete(DataAccesLayer.Document doc)
        {
            
            accesLayer.DeleteDocument(doc);
            listView.UpdateList();
        }

        public void RequestAccosiationRemoveToDocument(int docId, int empId)
        {
            DataAccesLayer.EmployeeDataAccesLayer empLayer = new DataAccesLayer.EQualDataLayerFactory ().GetEmployeeAccesLayer ();
            empLayer.RemoveEmployeeToBeNotified(docId, empId);
        }

        internal void RequestDocumentDelete(System.Collections.IList iList)
        {
            if (iList == null) return;
          //  new List<Document>(iEnumerable).ForEach(delegate(Document d) { if (d != null)RequestDocumentDelete(d); });
            foreach (Document doc in iList)
                RequestDocumentDelete(doc);
        }
    }

    public class SecondaryPresenter<T> 
    {
        public Action setUpAction;
        public  virtual void PushModelToView(object MainGridSelectedItem)
        {
            if (setUpAction != null) setUpAction();
        }
        public MainViewPresenter mainPresenter;
    }

    public class MainViewPresenter
    {
        private object mainGridSelectedItem;
        public event EventHandler  OnSelectedItemChanged;

        public ItemsListView view;



        public MainViewPresenter()
        {

        }

  
        public MainViewPresenter(ItemsListView view)
        {
            this.view = view;
          
        }


        private IEnumerable<DataAccesLayer.DomainModel.BaseDataAccesObject> itemsSource;
        public  IEnumerable<DataAccesLayer .DomainModel .BaseDataAccesObject > ItemsSource {
            get { return itemsSource; }
            set {


             //   DAOCollection<DataAccesLayer.DomainModel.BaseDataAccesObject> items = CreateObservableCollection(value);
                view.ItemsSource = value ;


                itemsSource  = value ;
                view.UpdateList();
            }
        }

        private DAOCollection<DataAccesLayer.DomainModel.BaseDataAccesObject> CreateObservableCollection(IEnumerable<DataAccesLayer .DomainModel .BaseDataAccesObject > value)
        {
            DAOCollection<DataAccesLayer.DomainModel.BaseDataAccesObject> items = new DAOCollection<DataAccesLayer.DomainModel.BaseDataAccesObject>(value);
            items.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(items_CollectionChanged);
            return items;
        }


        void items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {

        }
 
        protected   IList<SecondaryPresenter<object>> secondaryPresenters = new List<SecondaryPresenter<object>>();

        public void AddSecondaryPresenter(SecondaryPresenter<object> presenter)
        {
            secondaryPresenters.Add(presenter);
            presenter.mainPresenter = this;
        }

        public object MainGridSelectedItem
        {
            get { return mainGridSelectedItem; }
            set {

                mainGridSelectedItem = value;

                if (OnSelectedItemChanged != null)
                    OnSelectedItemChanged(this,new EventArgs ());

                SelectedItemChanged();
            }
        }


        /// <summary>
        /// 
        /// </summary>
        public void SelectedItemChanged()
        {
            if (MainGridSelectedItem  != null)
            {
                foreach (SecondaryPresenter<object> presenter in secondaryPresenters)
                {
                    presenter.PushModelToView(MainGridSelectedItem);
                }

            }
        }


    }

}
